package cmj.cloudmusic.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.musicmodule.Callback.Callback;
import com.example.musicmodule.entity.Music;
import com.example.musicmodule.util.MusicUrlUtil;
import com.example.musicmodule.util.RequestUtil;

import java.util.ArrayList;
import java.util.List;

import cmj.cloudmusic.R;
import cmj.cloudmusic.adapter.Music_ListView_Adapter;

/**
 * Created by 王虎 on 2016/12/22.
 */

public class RankItem_fragment extends Fragment {
    View view;
    Context context;
    ListView listView;
    List<Music> list;

    @Override
    public void onAttach(Context context) {
        this.context=context;
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.rankitem_fragment,null);
        listView= (ListView) view.findViewById(R.id.listview);
        new RequestUtil(getContext(), new Callback() {
            @Override
            public void callback(String s) {
                list=new ArrayList<Music>();
                JSONObject object= JSON.parseObject(s);
                JSONArray array=object.getJSONArray("song_list");
                list.addAll(JSON.parseArray(array.toJSONString(), Music.class));
                initListView();
            }
        }).getData(MusicUrlUtil.getMusicListByType(getTypeByPosition(getArguments().getInt("wang"))));
        //参数：	type =
        // 1-新歌榜,
        // 2-热歌榜,
        // 11-摇滚榜,
        // 12-爵士,
        // 16-流行,
        // 21-欧美金曲榜,
        // 22-经典老歌榜,
        // 23-情歌对唱榜,
        // 24-影视金曲榜,
        // 25-网络歌曲榜
        return view;
    }
    private  int getTypeByPosition(int i){
        switch (i){
            case 1:return 1;
            case 2:return 2;
            case 3:return 22;
            case 4:return 24;
            case 5:return 25;
            default:return 1;
        }
    }

    private void initListView(){
        RelativeLayout linearLayout= (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.rankitem_head,null);
        listView.addHeaderView(linearLayout);
        Music_ListView_Adapter adapter=new Music_ListView_Adapter(context,list);
        listView.setAdapter(adapter);
    }

}
